<template>
	<view class="cash">
		<view style="background: linear-gradient(to right, #f63c53 0%, #f76654 100%);height: 400upx;">
			<view style="font-size: 16px;color: #FFFFFF;padding-top: 100upx;">可提现总额</view>
			<view style="font-size: 29px;color: #FFFFFF;padding-top: 20upx;">¥ {{mayMoney}}</view>

			<view
				style="width: 90%;height: max-content;margin-left: 40upx;background-color: #FFFFFF;box-shadow: rgba(183, 183, 183, 0.3) 0px 1px 10px;margin-top: 50upx;border-radius: 20upx;">
				<view style="display: flex;flex-direction: row;padding: 20upx;">
					<view style="font-size: 16px;color: #333333;">提现金额</view>
					<!-- <view style="font-size: 11px;color: #333333;margin-left: 20upx;margin-top: 10upx;">{{min}}{{value}}元</view> -->
				</view>
				<view style="display: flex;flex-direction: row;padding: 20upx;">
					<view style="font-size: 14px;color: #333333;">¥</view>
					<input type="number" v-model="money" placeholder="请输入金额"
						style="font-size: 14px;color: #333333;text-align: left;margin-left: 10upx;width: 100%;" />
				</view>
				<view style="background: #E6E6E6;width: 100%;height: 1upx;"></view>

				<view style="display: flex;flex-direction: row;flex-wrap: wrap;">
					<view style="display: flex;flex-direction: row;" v-for="(item, index) in moneyList" :key="index">
						<view>
							<view style="padding: 20upx;" @click="getOut(item)">
								<view
									style="padding-top: 40upx;width: 180upx; height: 120upx;background-color: #FFFFFF;border:1px solid #FF332F;border-radius: 10upx;">
									{{ item }}
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
			<view style="width: 710upx;background: #FFFFFF;margin: 30upx 20upx;font-size: 28upx;"
				v-if="payList.length > 0">
				<view class="text-bold text-left margin-left">提现方式</view>
				<view style="display: flex;" v-for="(item,index) in payList" :key='index'>
					<view v-if="item.name === '微信' && wxSel != '否'"
						style="display: flex;width: 100%;padding: 20upx;height: 90rpx;margin: 0upx 0 0 30upx;align-items: center;">
						<image :src="item.image" style="width: 50upx;height: 50upx;border-radius: 50upx;"></image>
						<view style="color: #333333;margin-left: 20upx;width: 70%;text-align: left;">{{item.name}}
						</view>
						<radio-group name="openWay" style="margin-left: 40upx;" @tap='selectWay(item.id)'>
							<label class="tui-radio">
								<radio color="#FF332F" :checked="openWay === item.id ? true : false" />
							</label>
						</radio-group>
					</view>
					<view v-if="item.name === '银联' && checkYHK === '是'"
						style="display: flex;width: 100%;padding: 20upx;height: 90rpx;margin: 0upx 0 0 30upx;align-items: center;">
						<image :src="item.image" style="width: 50upx;height: 50upx;border-radius: 50upx;"></image>
						<view style="color: #333333;margin-left: 20upx;width: 70%;text-align: left;">{{item.name}}
						</view>
						<radio-group name="openWay" style="margin-left: 40upx;" @tap='selectWay(item.id)'>
							<label class="tui-radio">
								<radio color="#FF332F" :checked="openWay === item.id ? true : false" />
							</label>
						</radio-group>
					</view>
					<view v-if="item.name != '微信' && item.name != '银联'"
						style="display: flex;width: 100%;padding: 20upx;height: 90rpx;margin: 0upx 0 0 30upx;align-items: center;">
						<image :src="item.image" style="width: 50upx;height: 50upx;border-radius: 50upx;"></image>
						<view style="color: #333333;margin-left: 20upx;width: 70%;text-align: left;">{{item.name}}
						</view>
						<radio-group name="openWay" style="margin-left: 40upx;" @tap='selectWay(item.id)'>
							<label class="tui-radio">
								<radio color="#FF332F" :checked="openWay === item.id ? true : false" />
							</label>
						</radio-group>
					</view>
				</view>
			</view>
			<view @click="getOut(0)"
				style="margin: 32upx;font-size: 18px;background: #e64340;color: white;border-radius: 10px;height: 40px;line-height: 40px">
				提现
			</view>

			<view style="display: flex;width: 100%;justify-content: center;">
				<view style="color: grey;padding-bottom: 30px;padding-top: 20upx;flex: 1;" @click="goZhifuBao">提现账号
				</view>
				<view v-if="checkYHK === '是'" style="color: grey;padding-bottom: 30px;padding-top: 20upx;flex: 1;" @click="goYinHang">银联账号
				</view>
				<view style="color: grey;padding-bottom: 30px;padding-top: 20upx;flex: 1;" @click="list">提现记录</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				checkYHK: '否',
				wxSel: '否',
				money: '',
				zhifubao: '',
				mayMoney: '0',
				zhifubaoName: '',
				moneyList: [],
				value: 0,
				min: '',
				arr: [],
				showModal: true,
				payList: [],
				openWay: -1,
				bankCarUserName: '',
				bankCard: '',
				bankCardName: '',
			};
		},
		onLoad() {
			this.checkYHK = this.$queue.getData('checkYHK');
			this.$Request.getT('/common/type/296').then(res => { //完成
				if (res.status == 0) {
					if (res.data && res.data.value) {
						this.arr.push(res.data.value)
					}
				}
			})

			this.$Request.getT('/common/type/304').then(res => { //完成
				if (res.status == 0) {
					if (res.data && res.data.value) {
						this.moneyList = res.data.value.split(',');
					}
				}
			})

			this.$Request.getT('/common/type/305').then(res => { //完成
				if (res.status == 0) {
					if (res.data && res.data.value) {
						this.wxSel = res.data.value;
					}
				}
			})

			// #ifdef H5
			let target = navigator.userAgent.toLowerCase();
			let isWeixin = (/micromessenger/.test(target)) ? true : false;
			this.payList = [];
			if (isWeixin) {
				this.openWay = 2;
				let data = [{
					id: 2,
					image: 'https://api.shengqianxiong.com.cn/img/20201215/1137f42c4cf84c6f94f2662c8dabea8a.png',
					name: '支付宝'
				}, {
					id: 1,
					image: 'https://shop.shengqianxiong.com.cn/file/uploadPath/2022/11/28/0f62b4271ab218705e1c5cadb1584455.jpg',
					name: '银联'
				}, {
					id: 0,
					image: 'https://api.shengqianxiong.com.cn/img/20201216/206743676c7c4fb999ed1cb8c123a18b.png',
					name: '微信'
				}]
				this.payList = data;
			} else {
				this.openWay = 2;
				let data = [{
					id: 2,
					image: 'https://api.shengqianxiong.com.cn/img/20201215/1137f42c4cf84c6f94f2662c8dabea8a.png',
					name: '支付宝'
				}, {
					id: 1,
					image: 'https://shop.shengqianxiong.com.cn/file/uploadPath/2022/11/28/0f62b4271ab218705e1c5cadb1584455.jpg',
					name: '银联'
				}]
				this.payList = data;
			}
			// #endif
			// #ifdef APP-PLUS
			this.openWay = 2;
			let data = [{
				id: 2,
				image: 'https://api.shengqianxiong.com.cn/img/20201215/1137f42c4cf84c6f94f2662c8dabea8a.png',
				name: '支付宝'
			}, {
				id: 1,
				image: 'https://shop.shengqianxiong.com.cn/file/uploadPath/2022/11/28/0f62b4271ab218705e1c5cadb1584455.jpg',
				name: '银联'
			}, {
				id: 0,
				image: 'https://api.shengqianxiong.com.cn/img/20201216/206743676c7c4fb999ed1cb8c123a18b.png',
				name: '微信'
			}]
			this.payList = data;
			// #endif
			// #ifdef MP-WEIXIN
			this.openWay = 2;
			let data = [{
				id: 2,
				image: 'https://api.shengqianxiong.com.cn/img/20201215/1137f42c4cf84c6f94f2662c8dabea8a.png',
				name: '支付宝'
			}, {
				id: 1,
				image: 'https://shop.shengqianxiong.com.cn/file/uploadPath/2022/11/28/0f62b4271ab218705e1c5cadb1584455.jpg',
				name: '银联'
			}, {
				id: 0,
				image: 'https://api.shengqianxiong.com.cn/img/20201216/206743676c7c4fb999ed1cb8c123a18b.png',
				name: '微信'
			}]
			this.payList = data;
			// #endif
		},
		onShow: function(e) {
			this.getMoney();
			// #ifdef MP-WEIXIN
			let token = this.$queue.getData('token');
			//订阅
			if (token && !uni.getStorageSync('sendcashMsg')) {
				this.openMsg();
			}
			// #endif
		},
		onNavigationBarButtonTap() {
			this.list();
		},
		methods: {
			selectWay: function(id) {
				this.openWay = id;
			},
			openMsg() {
				var that = this
				wx.getSetting({
					withSubscriptions: true, //是否获取用户订阅消息的订阅状态，默认false不返回
					success(ret) {
						if (ret.subscriptionsSetting.itemSettings) {
							uni.setStorageSync('sendcashMsg', true)
							uni.openSetting({ // 打开设置页 
								success(rea) {
									console.log(rea.authSetting)
								}
							});
						} else { // 用户没有点击“总是保持以上，不再询问”则每次都会调起订阅消息
							uni.setStorageSync('sendcashMsg', false)
							uni.showModal({
								title: '提示',
								content: '为了更好的体验,请绑定消息推送',
								confirmText: '确定',
								cancelText: '取消',
								success: function(res) {
									if (res.confirm) {
										uni.requestSubscribeMessage({
											tmplIds: that.arr,
											success(re) {
												// console.log(re,'**********')
												var datas = JSON.stringify(re);
												if (datas.indexOf("accept") != -1) {
													console.log(re)
												}
											},
											fail: (res) => {
												console.log(res)
											}
										})
										that.showModal = false
									} else if (res.cancel) {
										that.showModal = true
									}
								}
							})
						}
					}
				})
			},
			list() {
				uni.navigateTo({
					url: '/pages/member/cashList'
				})
			},
			goYinHang() {
				uni.navigateTo({
					url: '/package/pages/index/yinhangka'
				});
			},
			goZhifuBao() {
				uni.navigateTo({
					url: '/pages/member/zhifubao'
				});
			},
			getMoney() {
				let that = this;
				let token = this.$queue.getData('token');
				let userId = this.$queue.getData('userId');
				if (token) {
					//this.$queue.showLoading("加载中...");
					//可以提现金额查询预估收入查询
					this.$Request.getT("/cash/money/" + userId).then(res => {
						if (res.status === 0 && res.data) {
							that.mayMoney = parseFloat(res.data).toFixed(2);
						} else if (res.status === -102) {
							this.$queue.showToast(res.msg);
							this.$queue.logout();
							uni.navigateTo({
								url: '/pages/member/register'
							});
						} else {
							that.mayMoney = '0.00';
							//this.$queue.showToast(res.msg);
						}
					});
					this.$Request.getT("/cash/userinfo/" + userId).then(res => {
						if (res.status === 0 && res.data) {
							that.zhifubao = res.data.zhifubao;
							that.zhifubaoName = res.data.zhifubaoName;

							if (res.data.bankCard) {
								that.bankCard = res.data.bankCard;
							}
							if (res.data.bankCardName) {
								that.bankCardName = res.data.bankCardName;
							}
							if (res.data.bankCarUserName) {
								that.bankCarUserName = res.data.bankCarUserName;
							}
						}
						uni.hideLoading();
					});
				}
			},
			//校验用户输入金额
			checkMobile(money) {
				return RegExp(/^1[34578]\d{9}$/).test(money);
			},
			getOut(money) {
				let that = this;
				let token = this.$queue.getData("token");
				let userId = this.$queue.getData("userId");
				let cashMoney = this.$queue.getData("cashMoney");
				if (token) {
					if (money && money != 0) {
						this.money = money;
					}
					if (!/^\d+$/.test(this.money)) {
						uni.showToast({
							icon: 'none',
							title: '请输入正确金额，不能包含中英文，特殊字符和小数'
						});
						return;
					}
					if (parseFloat(this.mayMoney).toFixed(1) >= parseFloat(this.money)) {
						if (parseFloat(this.money).toFixed(1) >= parseFloat(cashMoney)) {
							//0微信 1银行卡 2支付宝
							if (this.openWay == 0) {
								this.$queue.showLoading("提现中...");
								this.$Request.getT('/cash/out/' + userId + '/' + this.money + '?type=2')
									.then(
										res => {
											if (res.status === 0 && res.data) {
												that.$queue.showToast("提现申请成功，预计三个工作日到账");
												that.getMoney();
											}
											uni.hideLoading();
										});

							} else if (this.openWay == 1) {
								let contName = '';
								if (that.bankCard && that.bankCardName && that.bankCarUserName) {
									uni.showModal({
										title: "提现申请提示",
										content: '请仔细确认收款人信息\n收款银行:' + that.bankCardName + '\n金额:' + this.money +
											'\n收款账号：' + that.bankCard + '\n用户名：' + that.bankCarUserName,
										success: (e) => {
											if (e.confirm) {
												this.$queue.showLoading("提现中...");
												this.$Request.getT('/cash/out/' + userId + '/' + this.money +
														'?type=3')
													.then(
														res => {
															if (res.status === 0 && res.data) {
																that.$queue.showToast("提现申请成功，预计三个工作日到账");
																that.getMoney();
															}
															uni.hideLoading();
														});
											}
										}
									});

								} else {
									uni.navigateTo({
										url: "/package/pages/index/yinhangka"
									})
								}
							} else if (this.openWay == 2) {
								if (that.zhifubao && that.zhifubaoName) {
									uni.showModal({
										title: "提现申请提示",
										content: '请仔细确认收款人信息\n\n姓名:' + that.zhifubaoName + '\t\t金额:' + this.money +
											'\n\n收款账号：' + that.zhifubao + '',
										success: (e) => {
											if (e.confirm) {
												this.$queue.showLoading("提现中...");
												this.$Request.getT('/cash/out/' + userId + '/' + this.money +
														'?type=1')
													.then(
														res => {
															if (res.status === 0 && res.data) {
																that.$queue.showToast("提现申请成功，预计三个工作日到账");
																that.getMoney();
															}
															uni.hideLoading();
														});
											}
										}
									});

								} else {
									uni.navigateTo({
										url: "/pages/member/zhifubao"
									})
								}
							} else {
								this.$queue.showToast('请选择提现方式！');
							}
						} else {
							this.$queue.showToast("提现金额必须大于或等于" + cashMoney + "元才可提现");
						}
					} else {
						this.$queue.showToast("您的余额不足");
					}
				} else {
					uni.navigateTo({
						url: '/pages/member/register'
					});
				}
			},
			getOut1(money) {
				let that = this;
				let token = this.$queue.getData('token');
				let userId = this.$queue.getData('userId');
				if (token) {
					if (that.zhifubao && that.zhifubaoName) {
						if (parseFloat(money).toFixed(1) >= 10) {
							uni.showModal({
								title: '提现申请提示',
								content: '请仔细确认收款人信息\n\n姓名:' + that.zhifubaoName + '\t\t金额:' + money +
									'\n\n收款账号：' + that.zhifubao + '',
								success: e => {
									if (e.confirm) {
										this.$queue.showLoading('提现中...');
										this.$Request.getT('/cash/out/' + userId + '/' + money).then(res => {
											if (res.status === 0) {
												that.$queue.showToast('提现申请成功，预计三个工作日到账');
												that.getMoney();
											} else {
												uni.showModal({
													title: '温馨提示',
													content: res.msg,
													showCancel: false,
													cancelText: '取消',
													confirmText: '确认'
												});
											}
											uni.hideLoading();
										});
									}
								}
							});
						} else {
							this.$queue.showToast('提现金额必须大于或等于10元才可提现');
						}
					} else {
						uni.navigateTo({
							url: '/pages/member/zhifubao'
						});
					}
				} else {
					uni.navigateTo({
						url: '/pages/public/login'
					});
				}
			}
		}
	};
</script>

<style lang="less">
	@import '../../static/css/index.css';

	.view2-view-text {
		font-size: 14px;
		color: #000000;
		margin-left: 20upx;
		width: 80%;
	}

	.view2-view-image-right {
		width: 18upx;
		height: 30upx;
		margin-left: 50upx;
	}

	.cash {
		text-align: center;
		background: white;
		height: 100%;
		position: absolute;
		width: 100%;

		.cash-top {
			padding: 32upx 32upx 50upx 32upx;
			/* border-bottom: 1px solid gainsboro; */
			background: #e10a07;
		}

		.leiji {
			font-size: 14px;
			color: #ffffff;
			margin-bottom: 10px;
		}
	}
</style>
